//
//  SelectViewAlert.m
//  Skin
//
//  Created by MacOs on 16/6/22.
//  Copyright © 2016年 瑞恒信息科技股份有限公司. All rights reserved.
//

#import "SelectViewAlert.h"

@interface SelectViewTableCell : UITableViewCell

@property (nonatomic, strong) UILabel *text;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UIView *headLine;

@end
@implementation SelectViewTableCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self setBackgroundColor:[UIColor clearColor]];
        
        _text = [[UILabel alloc] initWithFrame:frame];
        _text.textAlignment = NSTextAlignmentCenter;
        [_text setFont:[UIFont systemFontOfSize:18]];
        [_text setTextColor:[UIColor colorWithRGB:0x12d2c4]];
        
        _headLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), 1)];
        [_headLine setHidden:YES];
        [_headLine setBackgroundColor:[UIColor colorWithRGB:0xEFEFEF]];

        _line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(frame)-1, CGRectGetWidth(frame), 1)];
        [_line setBackgroundColor:[UIColor colorWithRGB:0xEFEFEF]];
        [self addSubview:_line];
        [self addSubview:_headLine];
        [self addSubview:_text];

    }
    return self;
}

@end

#define DATE_ALERT_HEIGHT 160

@interface SelectViewAlert()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIButton      *backView;
@property (nonatomic, strong) UITableView   *tableView;

@end

@implementation SelectViewAlert

- (id)init
{
    self = [super initWithFrame:[self appRootViewController].view.bounds];
    if (self)
    {
        _backView = [[UIButton alloc] initWithFrame:self.bounds];
        [_backView addTarget:self action:@selector(backClickAction) forControlEvents:UIControlEventTouchUpInside];
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScreenHeight-DATE_ALERT_HEIGHT, kScreenWidth, DATE_ALERT_HEIGHT)];
        [_tableView setBackgroundColor:[UIColor whiteColor]];
        self.backgroundColor = [UIColor clearColor];
        
        [self addSubview: _backView];
        [self addSubview: _tableView];

    }
    return self;
}

- (UIViewController *)appRootViewController
{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
    
}

- (void)show
{
    [[self appRootViewController].view addSubview:self];

}

- (void)dissmis
{
    [self removeFromSuperview];
}

#pragma mark - action
- (void)backClickAction {
    [self dissmis];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _selectArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SelectViewTableCell *cell = [SelectViewTableCell new];
    [cell.text setText:_selectArray[indexPath.row]];
    if (indexPath.row == 0) {
        [cell.headLine setHidden:NO];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate alertSelectAction:self text:_selectArray[indexPath.row] index:indexPath.row];
    [self dissmis];
}

- (void)setSelectArray:(NSArray *)selectArray {
    _selectArray = selectArray;
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    NSInteger height = selectArray.count*44+1;
    if (height > DATE_ALERT_HEIGHT) {
        height = DATE_ALERT_HEIGHT+1;
    } else {
        _tableView.scrollEnabled = false;
    }
    
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self addSubview:_tableView];
    
    [_tableView reloadData];
    
    if (_selectValue) {
        for (int i = 0; i < selectArray.count; i++) {
            if ([_selectValue isEqualToString:_selectArray[i]]) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [_tableView selectRowAtIndexPath:indexPath animated:true scrollPosition:UITableViewScrollPositionTop];
                // 垂直居中
                CGPoint scrollPoint = _tableView.contentOffset;
                scrollPoint.y -= ((DATE_ALERT_HEIGHT-44)/2);
                [_tableView setContentOffset:scrollPoint animated:YES];
            }
        }
    }
    
}
@end
